//q7.cpp
//This program takes a range and return the amount of friendly numbers and the flawless numbers in that range.
//
//Binyamin 4 Jan 2018
//=======================================================================================================

#include<iostream>
#include<math.h>

using namespace std;

//sum_of_devisors
//intput: a number.
//output: the sum of devisors
int sum_of_devisors(int num)
{
	int i;
	int sum;
	sum=1;
	for(i=2;i<=(int)sqrt(num);i++)
		if(num%i == 0)
			sum+=i + num/i;
	return sum;
}
//is_flawless
//intput: a number
//output: whether that number is flawless
bool is_flawless(int num)
{
	return num == sum_of_devisors (num);
}
//count_flawless
//intput: a range starting at 2.
//output: the amount of fawless numbers in that range
int count_flawless(int n)
{
	int i, count;
	count=0;
	for(i=0;i<n;i++)
		if(is_flawless(i)) count++;
	return count;

}
//count_friends
//input: a range starting at 2.
//output: the amount of friendly numbers in that range.
int count_friends(int n)
{
	int i,count, potential;
	count = 0;
	for(i=2;i<=n;i++)
	{
		potential = sum_of_devisors(i);
		if((!is_flawless(i))&&(potential<=n))
		{
			if(sum_of_devisors(potential) == i)
				count++;
		}
	}
	return count;
}

//1. prompt for a number.
//2. capture that number.
//3. print the sum of visors for that number
//4. print whether the number is flawless.
//5. prompt for a range.
//6. capture range.
//7. print the amount of flawless and friends in that range.
int main()
{
	int num;
	cout << "Please enter a number" << endl;
	cin >> num;
	cout << "The sum of divsors for that number is " << sum_of_devisors(num) << endl;
	if(is_flawless(num)) cout << "The number is flawless" << endl;
	else cout << "The number is not flawless" << endl;
	cout << "Please enter a range for 2 to n" << endl;
	cin >> num;
	cout << "The amount of flawless numbers in that range is " << count_flawless(num) << endl \
		<< "The mount of friends in that range is " << count_friends(num) << endl;
}
//output
//Please enter a number 6
//The sum of divsors for that number is 6
//The number is flawless
//Please enter a range for 2 to n
//300
//The amount of flawless numbers in that range is 3
//The mount of friends in that range is 2
